package data

import android.content.Context
import android.content.SharedPreferences

class DataLocalManager private constructor(context: Context) {
    private val firstSharePreference = "FIRST_SHARED_PREFERENCES"

    private val mySharePreference: MySharePreference = MySharePreference(context)


    companion object {
        private lateinit var instance: DataLocalManager

        fun init(mContext: Context) {
            if (!Companion::instance.isInitialized) {
                instance = DataLocalManager(mContext)
            }
        }

        fun getInstance(): DataLocalManager {
            if (!Companion::instance.isInitialized) {
                throw UninitializedPropertyAccessException("DataLocalManager尚未初始化");
            }

            return instance
        }
    }

    fun setFirstInstalled(isFirst: Boolean) {
        mySharePreference.putBooleanValue(firstSharePreference, isFirst)
    }

    fun getFirstInstalled(): Boolean {
        return mySharePreference.getBooleanValue(firstSharePreference)
    }

    fun setSaveUserInfo(
        id: Long, name: String, userName: String,
        email: String, role: String
    ) {
        mySharePreference.saveUserInfo(id, name, userName, email, role)
    }

    fun removeValueFromSharedPreferences() {
        mySharePreference.removeValueFromSharedPreferences();
    }

    fun setSaveTokenKey(token: String) {
        mySharePreference.saveTokenKey(token)
    }

    fun getInfoUserId(): Long {
        return mySharePreference.getUserId()
    }

    fun getInfoName(): String? {
        return mySharePreference.getName()
    }

    fun getInfoUserName(): String? {
        return mySharePreference.getUserName()
    }

    fun getInfoEmail(): String? {
        return mySharePreference.getEmail()
    }

    fun getInfoUserRole(): String? {
        return mySharePreference.getUserRole()
    }

    fun getInfoTokenKey(): String? {
        return mySharePreference.getTokenKey()
    }


}

class MySharePreference(context: Context) {
    private val mySharePreferences = "MY_SHARED_PREFERENCES"
    private val userName = "USER_NAME"

    private val id = "ID"
    private val email = "EMAIL"
    private val name = "NAME"
    private val userRoles = "USER_ROLES"
    private val tokenKey = "TOKEN_KEY"

    private val sharedPreferences: SharedPreferences by lazy {
        context.getSharedPreferences(mySharePreferences, Context.MODE_PRIVATE)
    }

    // 在用户登录应用程序时保存信息
    fun putBooleanValue(key: String, value: Boolean) {
        sharedPreferences.edit().putBoolean(key, value).apply()
    }

    fun getBooleanValue(key: String): Boolean {
        return sharedPreferences.getBoolean(key, false)
    }

    fun saveUserInfo(
        id: Long, name: String, userName: String,
        email: String, role: String
    ) {
        sharedPreferences.edit().putLong(this.id, id).apply()
        sharedPreferences.edit().putString(this.name, name).apply()
        sharedPreferences.edit().putString(this.userName, userName).apply()
        sharedPreferences.edit().putString(this.email, email).apply()
        sharedPreferences.edit().putString(this.userRoles, role).apply()
    }

    fun saveTokenKey(token: String) {
        sharedPreferences.edit()
            .putString(this.tokenKey, token).apply()
    }

    fun removeValueFromSharedPreferences() {
//        sharedPreferences.edit()
//            .remove(this.id).apply()
        sharedPreferences.edit().apply {
            remove(id).apply()
            remove(name).apply()
            remove(userName).apply()
            remove(email).apply()
            remove(userRoles).apply()
        }
    }

    fun getUserId(): Long = sharedPreferences.getLong(id, 0)

    fun getName(): String? = sharedPreferences.getString(name, "")

    fun getUserName(): String? = sharedPreferences.getString(userName, "")

    fun getEmail(): String? = sharedPreferences.getString(email, "")

    // by default is "user" not "admin"
    fun getUserRole(): String? = sharedPreferences.getString(userRoles, "USER")

    fun getTokenKey(): String? =
        sharedPreferences.getString(tokenKey, "")

}